import '../mapper.dart';

final subTypeReg = RegExp(r'^([^<]*)<(.*)>$');

Map<Type, TypeBuilder> innerTypeBuilder() => <Type, TypeBuilder>{
      bool: QuickTypeBuilder<bool>(
        builder: (type) => false,
        toMap: (obj) async => obj,
        fromMap: (obj, map) async => switch (map) {
          null => null,
          num _ => map != 0,
          bool _ => map,
          String _ => map == 'true' || map == 'True' || map == '1' || map == 'TRUE' || map == 't',
          _ => throw ConverterException('$map is not a bool'),
        },
      ),
      int: QuickTypeBuilder<int>(
        builder: (type) => 0,
        toMap: (obj) async => obj,
        fromMap: (obj, map) async => switch (map) {
          null => null,
          String _ => int.parse(map),
          double _ => map.toInt(),
          bool _ => map ? 1 : 0,
          _ => map as int,
        },
      ),
      double: QuickTypeBuilder<double>(
        builder: (type) => 0,
        toMap: (obj) async => obj,
        fromMap: (obj, map) async => switch (map) {
          null => null,
          String _ => double.parse(map),
          int _ => map.toDouble(),
          bool _ => map ? 1.0 : 0.0,
          _ => map as double,
        },
      ),
      String: QuickTypeBuilder<String>(
        builder: (type) => "",
        toMap: (obj) async => obj,
        fromMap: (obj, map) async => switch (map) {
          null => null,
          String _ => map,
          _ => map.toString(),
        },
      ),
    };
